package com.angel.core.util;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.angel.health.PDFEntity;
import com.angel.provider.pdf.ParsingPdf;

public class RemotePDFParsingUtils {

	public static PDFEntity ParsingPDFToObject(String path) throws Exception{
		PDFEntity entity = new PDFEntity();
		InputStream  is = null;
			URL url = new URL(path);
			HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
			urlconn.setDoInput(true);
			urlconn.setDoOutput(true);
			urlconn.connect();
			is = new BufferedInputStream(urlconn.getInputStream());
			String pdfAbove = RemotePDFUtils.getTextFromPdf(is, false);
			entity = ParsingPdf.parsingAbove(pdfAbove);
			
			
			URL url2 = new URL(path);
			HttpURLConnection urlconn2 = (HttpURLConnection)url2.openConnection();
			urlconn2.setDoInput(true);
			urlconn2.setDoOutput(true);
			urlconn2.connect();
			is = new BufferedInputStream(urlconn2.getInputStream());
			
			String pdfBelow = RemotePDFUtils.getTextFromPdf(is,true);
			 Map<String, Object> parsingBelow = ParsingPdf.parsingBelow(pdfBelow);//体检结果
			 Map<String, Object> parsingTop = ParsingPdf.parsingTop(pdfBelow);//汇总分析、温馨提示  总检意见
			 entity.getBlobs().setInformation(JsonUtil.toJson(entity));
			 entity.getBlobs().setMedical(JsonUtil.toJson(parsingBelow));
			 entity.getBlobs().setSummary(JsonUtil.toJson(parsingTop));
			if(is != null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		return entity;
	}
}
